fname = input("请输入要写入的文件: ")
fo = open(fname, "w+")
ls = ["唐诗", "宋词", "元曲"]
fo.writelines(ls)
for line in fo:
    print(line)
fo.close()

# 另一种写法
# fname = input("请输入要写入的文件: ")
# ls = ["唐诗", "宋词", "元曲"]
# with open(fname, "w+") as fo:
#     fo.writelines(ls)
#     for line in fo:
#         print(line)


# open(fname, "w+")中w+，a等的区别